//
//  ECMaterailManageAddViewController.m
//  ECERP.CRM
//
//  Created by huanggulong on 2018/11/21.
//  Copyright © 2018 Mac. All rights reserved.
//

#import "ECMaterailManageAddViewController.h"
#import "ECMemberAddTableViewCell.h"
#import "ECButton.h"
#import "ECMaterailTypeListViewController.h"
#import "ECMaterailManageFieldListViewController.h"

@interface ECMaterailManageAddViewController ()<UITableViewDelegate,UITableViewDataSource> {
    NSInteger sectionShowSum;
    NSArray * sectionTitles;
}

@property (nonatomic,strong) UITableView * tableView;

@property (nonatomic,strong) NSArray<NSArray<NSMutableDictionary*>*> * dataArr;

@property (nonatomic,strong) NSMutableDictionary<NSIndexPath*,NSDictionary<NSString*,id>*> * nextSelctDic;

@property (nonatomic,strong) ECButton * showMoreBT;

@end

@implementation ECMaterailManageAddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"添加商品";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveClick:)];
}

#pragma mark - event
- (IBAction)saveClick:(id)sender{
    [MBProgressHUD showText:@"保存成功" toView:self.view];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.75 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.navigationController popViewControllerAnimated:YES];
    });
}

- (void)loadSubView {
    sectionShowSum = 2;
    sectionTitles = @[@"基本信息",@"单位信息",@"商品属性",@"价格信息",@"库存信息"];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(0);
    }];
}

- (void)showMorrInfo:(UIButton*)sender {
    sectionShowSum = self.dataArr.count;
    [self.tableView reloadData];
    self.tableView.tableFooterView.frame = CGRectMake(0, 0, 0, 0.0001);
    self.tableView.tableFooterView.hidden = YES;
}

#pragma mark -UITableViewDelegate,UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 27;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return sectionTitles[section];
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    UITableViewHeaderFooterView * v = (UITableViewHeaderFooterView *)view;
    v.backgroundView.backgroundColor = ECCOLOR(0xF4F4F4);
    v.textLabel.textColor = ECCOLOR(0xB3B3B3);
    v.textLabel.font = ECFONT(13 * PX_RELATIVE375);
    v.left = 18 * PX_RELATIVE375;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return sectionShowSum;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr[section].count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44 * PX_RELATIVE375;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ECMemberAddTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    NSMutableDictionary * dic = self.dataArr[indexPath.section][indexPath.row];
    cell.isNeed = [[dic objectForKey:@"isNeed"] boolValue];
    cell.cellStyle = [[dic objectForKey:@"style"] integerValue];
    cell.title = [dic objectForKey:@"title"];
    switch (cell.cellStyle) {
        case ECMemberAddCellStyleTxt:
        {
            if (!dic[@"value"] || ((NSString*)dic[@"value"]).length < 1) {
                cell.detailTF.placeholder = dic[@"placeholder"];
            }else{
                cell.detailTF.text = dic[@"value"];
            }
        }
            break;
        case ECMemberAddCellStyleMore: {
            if (!dic[@"value"] || ((NSString*)dic[@"value"]).length < 1) {
                cell.detailLB.text = dic[@"placeholder"];
                cell.detailLB.textColor = ECCOLOR(0xcccccc);
            }else{
                cell.detailLB.text = dic[@"value"];
                cell.detailLB.textColor = ECCOLOR(0x333333);
            }
        }
            break;
        case ECMemberAddCellStyleSwitch:{
            if ([dic[@"value"] boolValue]) {
                cell.isON = YES;
            }else {
                cell.isON = NO;
            }
        }
            break;
        default:
            break;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 1) {
            ECMaterailTypeListViewController *vc = [[ECMaterailTypeListViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }else if (indexPath.section == 1){
        ECMaterailManageFieldListViewController * vc = [[ECMaterailManageFieldListViewController alloc] init];
        if (indexPath.row == 0) {
            vc.fieldType = ECMaterailManageFieldTypeUnit1;
        }else if (indexPath.row == 1){
            vc.fieldType = ECMaterailManageFieldTypeUnit2;
        }else if (indexPath.row == 4){
            vc.fieldType = ECMaterailManageFieldTypeTaxRate;
        }else if (indexPath.row == 5){
            vc.fieldType = ECMaterailManageFieldTypeSortZone;
        }else if (indexPath.row == 2 || indexPath.row == 3 || indexPath.row == 6 || indexPath.row == 7){
            return;
        }
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.section == 2 && indexPath.row == 2){
        ECMaterailManageFieldListViewController * vc = [[ECMaterailManageFieldListViewController alloc] init];
        vc.fieldType = ECMaterailManageFieldTypeBrand;
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.section == 4 && indexPath.row == 3){
        ECMaterailManageFieldListViewController * vc = [[ECMaterailManageFieldListViewController alloc] init];
        vc.fieldType = ECMaterailManageFieldTypeStore;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark -lazyLoad

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ECSCREEN_WIDTH, 40 * PX_RELATIVE375)];
        [footer addSubview:self.showMoreBT];
        __weak typeof(footer) weakFooter = footer;
        [self.showMoreBT mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(weakFooter);
        }];
        _tableView.tableFooterView = footer;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0.00000001)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionHeaderHeight = 27 * PX_RELATIVE375;
        _tableView.sectionFooterHeight = 0.00001 * PX_RELATIVE375;
        [_tableView registerClass:[ECMemberAddTableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

- (ECButton *)showMoreBT {
    if (!_showMoreBT) {
        _showMoreBT = [ECButton new];
        _showMoreBT.splitBetweenImgAndTitle = 0 * PX_RELATIVE375;
        _showMoreBT.imgLocation = ECButonImageLocationRight;
        [_showMoreBT setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:0.001]];
        [_showMoreBT setTitle:@"扩展信息" forState:UIControlStateNormal];
        _showMoreBT.titleLabel.font = ECFONT(15*PX_RELATIVE375);
        [_showMoreBT setTitleColor:ECCOLOR(0x41D95A) forState:UIControlStateNormal];
        [_showMoreBT setImage:[UIImage imageNamed:@"moreInfo_member"] forState:UIControlStateNormal];
        [_showMoreBT addTarget:self action:@selector(showMorrInfo:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _showMoreBT;
}

- (NSArray<NSArray<NSDictionary *> *> *)dataArr {
    if (!_dataArr) {
        _dataArr = @[
                     @[@{@"title":@"预览图 *",@"isNeed":@(YES),@"placeholder":@"请选择图片",@"style":@(ECMemberAddCellStyleMore),@"value":@""}.mutableCopy,
                       @{@"title":@"商品类别 *",@"isNeed":@(YES),@"placeholder":@"请选择商品类别",@"style":@(ECMemberAddCellStyleMore),@"value":@""}.mutableCopy,
                       @{@"title":@"商品编码 *",@"isNeed":@(YES),@"placeholder":@"请输入手商品编码",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                       @{@"title":@"商品名称 *",@"isNeed":@(YES),@"placeholder":@"请输入详商品名称",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy],
                     
                     @[@{@"title":@"下单单位 *",@"isNeed":@(YES),@"placeholder":@"请选择存下单单位",@"style":@(ECMemberAddCellStyleMore),@"value":@""}.mutableCopy,
                       @{@"title":@"结算单位 *",@"isNeed":@(YES),@"placeholder":@"请选择价结算单位",@"style":@(ECMemberAddCellStyleMore),@"value":@""}.mutableCopy,
                       @{@"title":@"采购比例 *",@"isNeed":@(YES),@"placeholder":@"请输入采购比例",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                       @{@"title":@"发货比例 *",@"isNeed":@(YES),@"placeholder":@"请输入发货比例",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                       @{@"title":@"税率 *",@"isNeed":@(YES),@"placeholder":@"请选择价结税率",@"style":@(ECMemberAddCellStyleMore),@"value":@""}.mutableCopy,
                       @{@"title":@"分拣区域 *",@"isNeed":@(YES),@"placeholder":@"请选择价分拣区域",@"style":@(ECMemberAddCellStyleMore),@"value":@""}.mutableCopy,
                       @{@"title":@"状态",@"isNeed":@(NO),@"placeholder":@"正常",@"style":@(ECMemberAddCellStyleSwitch),@"value":@(YES)}.mutableCopy,
                       @{@"title":@"备注",@"isNeed":@(NO),@"placeholder":@"请输入备注",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy],
                     
                     @[@{@"title":@"规格",@"isNeed":@(NO),@"placeholder":@"请输入规格",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                       @{@"title":@"规格值",@"isNeed":@(NO),@"placeholder":@"请输入规格值",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                       @{@"title":@"品牌",@"isNeed":@(NO),@"placeholder":@"请选择品牌",@"style":@(ECMemberAddCellStyleMore),@"value":@""}.mutableCopy,
                       @{@"title":@"助记码",@"isNeed":@(NO),@"placeholder":@"请输入企助记码",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                       @{@"title":@"级别",@"isNeed":@(NO),@"placeholder":@"请输入级别",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                       @{@"title":@"产地",@"isNeed":@(NO),@"placeholder":@"请输入产地",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy],
                     
                     @[@{@"title":@"销售基准价",@"isNeed":@(NO),@"placeholder":@"请输入销售基准价",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                       @{@"title":@"含税采购价",@"isNeed":@(NO),@"placeholder":@"请输入含税采购价",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                       @{@"title":@"市场价",@"isNeed":@(NO),@"placeholder":@"请输入市场价",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                       @{@"title":@"价格系数",@"isNeed":@(NO),@"placeholder":@"请输入价格系数",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                       @{@"title":@"超市价",@"isNeed":@(NO),@"placeholder":@"请输入超市价",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy],
                     
                     @[@{@"title":@"警戒库存",@"isNeed":@(NO),@"placeholder":@"请输入警戒库存",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                     @{@"title":@"安全库存",@"isNeed":@(NO),@"placeholder":@"请输入安全库存",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                       @{@"title":@"初始库存",@"isNeed":@(NO),@"placeholder":@"请输入初始库存",@"style":@(ECMemberAddCellStyleTxt),@"value":@""}.mutableCopy,
                       @{@"title":@"存储方式",@"isNeed":@(NO),@"placeholder":@"请选择存储方式",@"style":@(ECMemberAddCellStyleMore),@"value":@""}.mutableCopy]];
    }
    return _dataArr;
}

@end
